package com.hsy.baoinbao.modules.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.login.gsondata.LoginResultData;
import com.hsy.baoinbao.modules.main.ui.activity.MainActivity;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/29.
 */
public class ForgetPasswordActivity extends CommonBaseActivityWithSimpleActionBar {
    private EditText phone_edit, code, password_edit, password_edit_two;
    private TextView change_phone_get_code, finish;
    private final long TIMER_LENGTH = 60000l;
    private final long TIMER_SEQUENCE = 1000l;
    private TimeCount mTimeCount;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                DPLogUtil.d(response);
                CommonUtils.hideProgressDialog();
                finish();
            }
            if (1 == what) {
                DPLogUtil.d(response);
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            if (0 == what) {
                CommonUtils.hideProgressDialog();
            }
        }
    };

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        setTitle("找回密码");
        phone_edit = F(R.id.phone_edit);
        code = F(R.id.code);
        password_edit = F(R.id.password_edit);
        password_edit_two = F(R.id.password_edit_two);

        change_phone_get_code = F(R.id.change_phone_get_code);
        finish = F(R.id.finish);

        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBeforePost()) {
                    CommonUtils.showProgressDialog(ForgetPasswordActivity.this);
                    Map<String, String> params = new HashMap<>();
                    params.put(Consts.USER_NAME, phone_edit.getText().toString().trim());
                    params.put(Consts.USER_PASSWORD, password_edit.getText().toString().trim());
                    params.put("smscode", code.getText().toString().trim());
                    HttpUtil.httpPostWithParams(ForgetPasswordActivity.this, 0, Consts.API_FORGET_PASSWORD, params, callback);
                }
            }
        });
        change_phone_get_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEmpty(phone_edit.getText().toString())) {
                    showToast("请填写手机号");
                    return;
                } else {
                    mTimeCount = new TimeCount(TIMER_LENGTH, TIMER_SEQUENCE);
                    mTimeCount.start();
                    Map<String, String> data = new HashMap<>();
                    data.put("mobile", phone_edit.getText().toString());
                    HttpUtil.httpPostWithParams(ForgetPasswordActivity.this, 1, Consts.API_SEND_SMS, data, callback);
                }
            }
        });
    }

    private boolean checkBeforePost() {
        if (isEmpty(phone_edit.getText().toString())) {
            showToast("请填写手机号");
            return false;
        } else if (isEmpty(code.getText().toString())) {
            showToast("请输入验证码");
            return false;
        } else if (isEmpty(password_edit.getText().toString())) {
            showToast("请填写密码");
            return false;
        } else if (isEmpty(password_edit_two.getText().toString())) {
            showToast("请确认密码");
            return false;
        } else if (!password_edit.getText().toString().equals(password_edit_two.getText().toString())) {
            showToast("两次密码不一致");
            return false;
        }

        return true;
    }

    class TimeCount extends CountDownTimer {
        String sendAgain = "重新获取";

        boolean isRunning = false;

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (millisUntilFinished / 1000 == 0) {
                onFinish();
            } else {
                isRunning = true;
                change_phone_get_code.setText(sendAgain + "(" + millisUntilFinished / 1000 + "s" + ")");
                change_phone_get_code.setClickable(false);
                change_phone_get_code.setBackgroundResource(R.drawable.frame_person_verification_code_gary);
            }
        }

        @Override
        public void onFinish() {
            isRunning = false;

            String str = "获取验证码";
            change_phone_get_code.setText(str);
            change_phone_get_code.setClickable(true);
            change_phone_get_code.setBackgroundResource(R.drawable.frame_person_verification_code);
        }
    }
}
